import services from '../service';
import _ from 'lodash';
import { Model, ItemUtils, unitsPrice } from '@/utils'
import { filterCondition } from '../config/filterConfig'
import {  tableList2 } from '../config/tableFields'
import { base } from '@/config';
import { message } from 'antd'
import pageConfig from '../config/pageConfig';
// 模拟数据引入
import { STATUSENUM,CHANNELENUM,PAYENUM } from '../config/enum';
const { namespace, listenRouter } = pageConfig;
const initState = {
  filterCondition: filterCondition, // 列表-筛选
  fields: tableList2, // 列表-表格
  pagination: { // 分页
    ...base.initPage,
  },

  list: [], // 列表-数据
  filterParams: {}, // 筛选条件

  selectedRowKeys: [],  // 选择的key
  selectedRows: [],     // 选择的数据
  isCheckedAll: false,  // 是否全选
  // 选择枚举
  statusKey: undefined, // 订单顶部选项卡选中数据
  // 状态枚举
  statusEnum: [], // 列表顶部状态枚举
  channelEnum: [], // 交易渠道枚举
  payEnum: [], // 支付方式枚举
  isEnumLoading: true, // 枚举获取是否成功
}

export default Model.extend({
  namespace: namespace,
  state: _.cloneDeep(initState),
  subscriptions: {
    setup({ dispatch, listen }: any) {
      listen(listenRouter, () => {
        dispatch({
          type: 'updateState',
          payload: {
            ..._.cloneDeep(initState),
          }
        })
        dispatch({ type: 'fetchStatusEnum' });
      });
    }
  },
  effects: {
    // 进入页面默认请求
    *fetchStatusEnum({ payload }: any, { call, select, update,put }: any) { 
      // const data = yield call(services.fetchSyncList, {});
      // 订单状态枚举
      let data = STATUSENUM;
      // 交易渠道枚举
      let channelData = CHANNELENUM;
      // 支付方式
      let payData = PAYENUM
      if (Array.isArray(data) && data.length > 0) {
        yield update({
          statusEnum: data,
          statusKey: data[0].value,
          payEnum: payData,
          channelEnum: channelData,
        })
        yield put('updateFilterConditon')
        yield put('fetchList')
      } else {
        message.error('订单状态获取失败')
      }
    },
    // 点击选项卡进行对应数据切换
    *fetchCheckedListData({ payload }: any, { callWithLoading, select, update, put }: any) { 
      const { statusKey } = payload;
      yield update({
        statusKey
      })
      yield put('fetchList')
    },
    // 搜索列表
    * fetchList({ payload }: any, { callWithSpinning, select, update }: any) {
      const {
        pagination: { ps, pn },
        filterParams,
        statusKey
      } = yield select((_: any) => _[namespace]);

      const searchParams = {
        ps,
        pn,
        ...filterParams,
        ...payload,
        statusKey
      }
      console.log(searchParams);
      
      // const data = yield callWithSpinning(services.fetchSyncList, searchParams);
      const data = {
        datas: [{
          skuName: '2e',
          key: '1',
          id:'1'
        }],
        pn: 1,
        ps: 1,
        tc:1
      }
      if (data) {
        const { datas, pn, ps, tc } = data;
        yield update({
          isEnumLoading: false, // 枚举获取是否成功
          list: datas,
          pagination: {
            ps,
            pn,
            total: tc,
            current: pn, // 组件需要
            pageSize: ps, // 组件需要
          },
        });
      }
    },

  },
  reducers: {
    // 更新筛选条件配置
    updateFilterConditon(state: any, {payload}: any) {
      const {
        filterCondition,
        channelEnum,
        payEnum
      } = state;

      const _filterCondition = ItemUtils.getItemType(_.cloneDeep(filterCondition))
        .extend([
          {
            title: 'channelId',
            selectCondition: channelEnum,
          },
          {
            title: 'pay',
            selectCondition: payEnum,
          },
        ]).values()
      return {
        ...state,
        filterCondition: _filterCondition,
      }
    },
  }
})